<template>
  <div class="channel-edit">
    <van-cell>
      <div slot="title" class="title-text">我的频道</div>
      <van-button size="mini" plain type="primary" class="edit-btn" round @click="isEdit = !isEdit"
        >{{isEdit?'完成':'编辑'}}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" column-num="4" class="my-grid">
      <van-grid-item
        v-for="(channel,index) in myChannels"
        :key="index"
        class="grid-item"
        icon="clear"
        :class="{isEdit:isEdit&& ! fixedChannels.includes(channel.id)}"
        @click="onMyChannelClick(channel,index)"
      >
      <span class="text" slot="text" :class="{active :index === acticveIndex }">{{channel.name}}</span>
      </van-grid-item>
    </van-grid>

    <van-cell>
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" column-num="4" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel,index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channel.name"
        @click="addItme(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { grtChannels, addChannels, deleChannelsByid } from '@/api/channels.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      require: true
    },
    acticveIndex: {
      type: Number,
      require: true
    }
  },
  components: {},
  data () {
    return {
      allchannels: [],
      isEdit: false,
      fixedChannels: [0]
    }
  },
  async created () {
    const { data: { data: { channels } } } = await grtChannels()
    this.allchannels = channels
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allchannels.filter(t => {
        const f = this.myChannels.find(m => t.id === m.id)
        return !f
      })
    }
  },
  methods: {
    async  addItme (channel) {
      this.myChannels.push(channel)
      if (this.user) {
        // 已登录
        await addChannels({ id: channel.id, seq: this.myChannels.length })
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    async onMyChannelClick (channle, index) {
      if (this.isEdit) {
        // 删除
        if (this.fixedChannels.includes(channle.id)) {
          return
        }
        if (index <= this.acticveIndex) {
          this.$emit('update-active', this.acticveIndex - 1)
        }
        this.myChannels.splice(index, 1)
        if (this.user) {
        // 已登录
          await deleChannelsByid(channle.id)
        } else {
        // 未登录
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } else {
        // console.log(index)
        this.$emit('update-active', index, false)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
}
.title-text {
  font-size: 32px;
  color: #333333;
}
.edit-btn {
  width: 104px;
  height: 48px;
  font-size: 26px;
  color: #f85959;
  border: 1px solid #f85959;
}
/deep/ .grid-item {
  width: 160px;
  height: 86px;
  .van-grid-item__content {
    white-space: nowrap;
    background-color: #f4f5f6;
    .van-grid-item__text,
    .text {
      font-size: 28px;
      color: #222;
      margin-top: 0;
    }
    .active{
        color: skyblue;
    }
  }
}
/deep/ .my-grid {
  .grid-item {
    .van-icon-clear {
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 30px;
      color: #cacaca;
      z-index: 2;
    }
  }
}
/deep/ .recommend-grid {
  .grid-item {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }
}
/deep/ .van-icon-clear{
  display: none;
}
/deep/ .isEdit .van-icon-clear{
  display: block;
}
</style>
